import Variable from "../pages/constants/index";
import { ResponseData } from "../pages/types/common";
import { HttpMethod } from "../pages/types/enum";
// import { HttpMethod, ResponseData } from "../pages/types/common";

//  interface ResponseData<T> {
//   code: number
//   msg: string
//   data: T
// }

// // 枚举类型
//  enum HttpMethod {
//   GET = "GET", // 默认的枚举 对应的是数字 这么一改对应的是字符串
//   POST = "POST",
//   PUT = "PUT",
//   DELETE = "DELETE"
// }

export default class RequestHttp{
  static get<T>(url:string,params?:object){
    return request<T>(url,HttpMethod.GET,params)
  }
  static post(){}
  static put(){}
  static delete(){}
}

function request<T> (url:string,method:HttpMethod,data?:object){
  return new Promise((resolve,reject)=>{
    wx.request<ResponseData<T>>({
      url:Variable.BASE_URL + url,
      method,
      data,
      success(res){
        if(res.data.code === 200){
          resolve(res.data.data)
        }else{
          wx.showToast({
            title:res.data.msg,
            icon:'error'
          })
          reject(new Error(res.data.msg))
        }
      },
      fail(res){
        reject(new Error(res.errMsg))
      }
    })
  })
}